package com.magicborrow.fragment;

import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.support.v7.widget.GridLayoutManager;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.RelativeLayout;
import android.widget.Toast;

import com.android.volley.VolleyError;
import com.magicborrow.Constants;
import com.magicborrow.R;
import com.magicborrow.adapter.PublishedRentAdapter;
import com.magicborrow.beans.ConllectionListBean;
import com.magicborrow.beans.IPublishBean;
import com.magicborrow.beans.StuffBean;
import com.magicborrow.utils.UIHelper;
import com.magicborrow.utils.UserTools;
import com.magicborrow.utils.VolleyTool;
import com.magicborrow.views.RefreshRecyclerView;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.Map;


/**
 * Created by River on 5/11/16.
 */
public class PublishedRentFragment extends Fragment implements VolleyTool.HTTPListener{

    private RelativeLayout mFragmentLayout;
    private RefreshRecyclerView mRefreshRecyclerView;
    private GridLayoutManager mGridLayoutManager;
    private PublishedRentAdapter mPublishedRentAdapter;
    private ArrayList<StuffBean> mData;
    private int page = 0;
    private int size = 10;

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
        mFragmentLayout = (RelativeLayout) inflater.inflate(R.layout.published_rent_fragment,null);
        initView();
        initListener();
        initData();
        sendRequest();
        return mFragmentLayout;
    }

    private void initView(){
        mRefreshRecyclerView = (RefreshRecyclerView)mFragmentLayout.findViewById(R.id.refreshRecyclerView);
        mGridLayoutManager = new GridLayoutManager(getActivity(),2);
        mRefreshRecyclerView.setLayoutManager(mGridLayoutManager);
    }

    private void initData(){
        mData = new ArrayList<>();
        mPublishedRentAdapter = new PublishedRentAdapter(mData);
        mPublishedRentAdapter.setWidth(UIHelper.getScreenWidth(getActivity())/2);
        mRefreshRecyclerView.setAdapter(mPublishedRentAdapter);
    }

    private void initListener(){
        mRefreshRecyclerView.setOnRefreshListener(new RefreshRecyclerView.OnRefreshListener() {
            @Override
            public void onRefreshListener() {
                page = 0;
                sendRequest();
            }
        });
        mRefreshRecyclerView.setOnPullListener(new RefreshRecyclerView.onPullListener() {
            @Override
            public void onPullListener() {
                page++;
                sendRequest();
            }
        });
    }

    private void sendRequest(){
        Map<String,String> params = new HashMap<>();
        params.put("page",page+"");
        params.put("size",size+"");
        params.put("userId", UserTools.getUser(getActivity()).getId()+"");
        VolleyTool.get(Constants.GET_PUBLISH_LIST_URL,params,this,0, IPublishBean.class);
    }

    @Override
    public <T> void onResponse(T t, int requestCode) {
        IPublishBean bean = (IPublishBean) t;
        if (bean.getCode() == 0) {
            mRefreshRecyclerView.setLoadFinish();
            if (page == 0) {
                mPublishedRentAdapter.getData().clear();
                mData.addAll(bean.getData().getContent());
                mPublishedRentAdapter.notifyDataSetChanged();
            } else {
                if (bean.getData().getContent().size() > 0) {
                    int startPosition = mPublishedRentAdapter.getItemCount();
                    mPublishedRentAdapter.getData().addAll(bean.getData().getContent());
                    mPublishedRentAdapter.notifyItemRangeInserted(startPosition, bean.getData().getContent().size());
                } else {
                    mRefreshRecyclerView.setIsLastPage(true);
                    mRefreshRecyclerView.setLoadFinish();
                }
            }
        } else {
            Toast.makeText(getActivity(),bean.getMessage(),Toast.LENGTH_SHORT).show();
        }
    }

    @Override
    public void onErrorResponse(VolleyError error, int requestCode) {
        Toast.makeText(getActivity(),"服务器暂时无法连接",Toast.LENGTH_SHORT).show();
    }

}
